package com.yuhuihui.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.function.Function;

/**
 * 字符串工具类
 *
 * @author yuhh
 * @date 2023-05-25 14:42:41
 */
public final class StringUtil {

    /**
     * 随机字符
     */
    private static final char[] STR = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
            'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
            'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
            's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
            '3', '4', '5', '6', '7', '8', '9' };

    /**
     * 获取指定长度的随机字符串
     *
     * @param length 随机字符串长度
     * @return {@link String } 随机字符串
     * @author yuhh
     * @date 2023-05-25 14:42:46
     */
    public static String genRandomString(int length){
        int  maxNum = 62;
        StringBuilder rs = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < length; i ++){
            rs.append(STR[r.nextInt(maxNum)]);
        }
        return rs.toString();
    }

    /**
     * 左边补空格
     *
     * @param content 需要补空格的内容
     * @param length  补完空格后的长度
     * @return {@link String } 补完空格后的内容
     * @author yuhh
     * @date 2024-09-12 11:40:01
     */
    public static String fillInTheSpaceOnTheLeft(String content, int length){
        int l = content.length();
        if (l < length){
            return  fillInTheSpace(length - l) + content;
        }
        return content;
    }

    /**
     * 右边补空格
     *
     * @param content 需要补空格的内容
     * @param length  补完空格后的长度
     * @return {@link String } 补完空格后的内容
     * @author yuhh
     * @date 2023-05-25 14:42:51
     */
    public static String fillInTheSpaceOnTheRight(String content, int length){
        int l = content.length();
        if (l < length){
            return content + fillInTheSpace(length - l);
        }
        return content;
    }

    /**
     * 左边补0
     *
     * @param content 需要补0的内容
     * @param length  补完0后的长度
     * @return {@link String } 补完0后的内容
     * @author yuhh
     * @date 2023-05-25 14:42:56
     */
    public static String zeroPaddingOnTheLeft(String content, int length){
        int l = content.length();
        if (l < length){
            return zeroPadding(length - l) + content;
        }
        return content;
    }

    /**
     * 右边补0
     *
     * @param content 需要补0的内容
     * @param length  补完0后的长度
     * @return {@link String } 补完0后的内容
     * @author yuhh
     * @date 2024-09-12 11:39:10
     */
    public static String zeroPaddingOnTheRight(String content, int length){
        int l = content.length();
        if (l < length){
            return  content + zeroPadding(length - l);
        }
        return content;
    }

    /**
     * String拆分成list
     *
     * @param toSplit   需要拆分的字符串
     * @param delimiter 分隔符
     * @param mapper    映射器
     * @return {@link List }<{@link T }>
     * @author yuhh
     * @date 2023-05-25 16:32:42
     */
    public static <T> List<T> stringToList(String toSplit, String delimiter, Function<? super String, ? extends T> mapper){
        StringTokenizer stringTokenizer = new StringTokenizer(toSplit, delimiter);
        List<T> list = new ArrayList<>();
        while(stringTokenizer.hasMoreTokens()) {
            list.add(mapper.apply(stringTokenizer.nextToken()));
        }
        return list;
    }

    /**
     * String拆分成list
     *
     * @param toSplit   需要拆分的字符串
     * @param delimiter 分隔符
     * @return {@link List }<{@link String }>
     * @author yuhh
     * @date 2023-05-25 16:44:47
     */
    public static List<String> stringToList(String toSplit, String delimiter){
        StringTokenizer stringTokenizer = new StringTokenizer(toSplit, delimiter);
        List<String> list = new ArrayList<>();
        while(stringTokenizer.hasMoreTokens()) {
            list.add(stringTokenizer.nextToken());
        }
        return list;
    }

    /**
     * 补空格
     *
     * @param length 需要补空格的长度
     * @return {@link String } 补完后的内容
     * @author yuhh
     * @date 2023-05-25 14:43:01
     */
    private static String fillInTheSpace(int length){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++){
            sb.append(" ");
        }
        return sb.toString();
    }

    /**
     * 补0
     *
     * @param length 需要补0的长度
     * @return {@link String } 补完后的内容
     * @author yuhh
     * @date 2023-05-25 14:43:06
     */
    private static String zeroPadding(int length){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++){
            sb.append("0");
        }
        return sb.toString();
    }
}
